package app.oa.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Servlet Filter implementation class SetCharCodeFilter
 */
public class CharCodeFilter implements Filter {
	
	 protected String encoding = null;   
	 protected FilterConfig filterConfig = null;

    /**
     * Default constructor. 
     */
    public CharCodeFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		this.encoding = null;   
        this.filterConfig = null; 
	}
	
	class Request extends HttpServletRequestWrapper {   
		  
        public Request(HttpServletRequest request) {   
            super(request);   
         }   
        /**
          * 转换由表单读取的数据的内码.
          * 从 ISO 字符转到 utf-8(或gbk).
          */  
        public String toChi(String input) {   
            try {   
                   
              byte[] bytes = input.getBytes("ISO-8859-1");   
              return new String(bytes, encoding);   
             }   
            catch (Exception ex) {   
             }   
            return null;   
         }   
  
        /**
          * Return the HttpServletRequest holded by this object.
          */  
        private HttpServletRequest getHttpServletRequest()   
         {   
            return (HttpServletRequest)super.getRequest();   
         }   
  
        /**
          * 读取参数 -- 修正了中文问题.
          */  
        public String getParameter(String name)   
         {   
            return toChi(getHttpServletRequest().getParameter(name));   
         }   
  
        /**
          * 读取参数列表 - 修正了中文问题.
          */  
        public String[] getParameterValues(String name)   
         {   
               String values[] = getHttpServletRequest().getParameterValues(name);   
              if (values != null) {   
                for (int i = 0; i < values.length; i++) {   
                   values[i] = toChi(values[i]);   
                 }   
               }   
              return values;   
         }   
     }   

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {
		response.setCharacterEncoding(encoding);
		HttpServletRequest httpreq = (HttpServletRequest)request;   
	    if(httpreq.getMethod().equals("POST")) {   
	            request.setCharacterEncoding(encoding);
	    } else {   
	        request = new Request(httpreq);   
	    }   
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.filterConfig = fConfig;   
        this.encoding = "UTF-8";  
	}

}
